package com.tgpms.web.cost.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.tgpms.web.cost.entity.SubProjects;
import com.tgpms.web.cost.service.SubProjectsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.tgpms.auth.security.TokenService;
import com.tgpms.common.Result;
import com.tgpms.exception.DeleteException;
import com.tgpms.exception.ExceptionExplain;
import com.tgpms.exception.InsertException;
import com.tgpms.exception.QueryException;
import com.tgpms.exception.UpdateException;
import com.tgpms.util.Common;
import com.tgpms.util.PageInfo;
import com.tgpms.util.PageView;
import com.tgpms.web.cost.entity.DetailEstimateItems;
import com.tgpms.web.cost.entity.DetailEstimateItemsKey;
import com.tgpms.web.cost.entity.ProjectEstimate;
import com.tgpms.web.cost.service.DetailEstimateItemsService;
import com.tgpms.web.cost.service.ProjectEstimateService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * @author newway
 * <p>
 * 工程概算汇总和细项 相关接口
 */
@Api(value = "ProjectEstimateController", tags = "工程概算汇总和细项 相关接口")
@RestController
@RequestMapping(value = "/ev/projectEstimate")
public class ProjectEstimateController {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ProjectEstimateService projectEstimateService;
	@Autowired
	private DetailEstimateItemsService detailEstimateItemsService;
	@Autowired
	private TokenService tokenService;
	@Resource
	private SubProjectsService subProjectsService;

	
	/**
	* @Author newway
	* @Description 查询工程概算汇总信息接口信息
	* @Date 2019年8月2日上午8:21:38
	* @Param pageNo
	* @Param sortByList {"sortby":"字段","order":"asc"}
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "查询工程概算汇总信息接口信息", notes = "查询工程概算汇总信息接口信息", httpMethod = "POST")
	@PostMapping(value = "/selProjectEstimateList")
	public Result selProjectEstimateList(@RequestParam(value = "selparam", required = false) String selparam,
			@RequestParam(value = "leftdition", required = false) String leftdition,
		    @RequestParam(value = "condition", required = false) String condition,
		    @RequestParam(value = "dataowner", required = false) String dataowner,
			@RequestParam(value = "pageNo", required = true) String pageNo,
			@RequestParam(value = "pageSize", required = true) String pageSize) {
		Result result = new Result();
		PageInfo pageInfo = new PageInfo();
		if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			pageInfo.setPage(pageNo);
			pageInfo.setSize(pageSize);
			Map<String,String> map = new HashMap<String,String>();
			map.put("selparam", selparam);
			map.put("leftdition", leftdition);
			map.put("condition", condition);
			map.put("dataowner", dataowner);
			pageInfo.setQueryMap(map);
			result = projectEstimateService.selProjectEstimateList(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
			throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
		}
		return result;
	}
	
	
	/**
	* @Author newway
	* @Description  获取概算汇总主键接口
	* @Date 2019年8月29日下午4:20:13
	* @Param estimateHierarchyCode 概算层次代码
	* @Param wbsCode WBS代码
	* @Param cwpCode 项目代码
	* @return 
	**/
	@ApiOperation(value = "获取概算汇总主键接口", notes = "获取概算汇总主键接口", httpMethod = "POST")
	@PostMapping(value = "/getEstimatePcsCode")
	public Result getEstimatePcsCode(@RequestParam(value = "estimateHierarchyCode", required = true) String estimateHierarchyCode,
			@RequestParam(value = "wbsCode", required = true) String wbsCode,
			@RequestParam(value = "cwpCode", required = true) String cwpCode) {
		Result result = new Result();
		try {
			estimateHierarchyCode = Common.rightSupplement(estimateHierarchyCode, " ", 14-estimateHierarchyCode.length());
			if(wbsCode.length()<4) {
				wbsCode = Common.rightSupplement(wbsCode, " ", 4-wbsCode.length());
			}
			if(cwpCode.length()!=3) {
				result.setSuccess(false);
				result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
				return result;
			}
			String pcsCode = estimateHierarchyCode+"-"+wbsCode+"-"+cwpCode;
			result.setSuccess(true);
			result.setMsg("成功");
			result.setData(pcsCode);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_PARSING.getExplain());
		}
		return result;
	}
	
	
	/**
	* @Author newway
	* @Description 添加工程概算汇总信息接口
	* @Date 2019年8月1日下午4:22:08
	* @Param com.tgpms.web.cost.entity.ProjectEstimate
	* @Param estimateCode 概算层次代码
	* @Param wbsCode WBS代码
	* @Param projectCode 项目代码
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "添加工程概算汇总信息接口", notes = "添加工程概算汇总信息接口", httpMethod = "POST")
	@PostMapping(value = "/addProjectEstimateInfo")
	public Result addProjectEstimateInfo(@RequestBody ProjectEstimate projectEstimate,HttpServletRequest request) {
		Result result = new Result();
		try {
			if(Common.isEmpty(projectEstimate.getEstimateHierarchyCode()) || 
					Common.isEmpty(projectEstimate.getWbsCode()) || Common.isEmpty(projectEstimate.getCwpCode()) || 
					Common.isEmpty(projectEstimate.getPcsCode())) {
				result.setSuccess(false);
				result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
				return result;
			}
			//获取token操作人
			projectEstimate.setInsertUser(tokenService.getUserName(request));
			projectEstimate.setInsertDate(new Date());
			Map<String,String> map = projectEstimateService.addProjectEstimateInfo(projectEstimate);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
				result.setData(projectEstimate.getPcsCode());
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	
	/**
	* @Author newway
	* @Description 更新工程概算汇总信息接口
	* @Date 2019年8月1日下午5:37:08
	* @Param com.tgpms.web.cost.entity.ProjectEstimate
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "更新工程概算汇总信息接口", notes = "更新工程概算汇总信息接口", httpMethod = "PUT")
	@PutMapping(value = "/updateProjectEstimateInfo")
	public Result updateProjectEstimateInfo(@RequestBody ProjectEstimate projectEstimate,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(projectEstimate.getPcsCode())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			//获取token操作人
			projectEstimate.setModifyUser(tokenService.getUserName(request));
			projectEstimate.setModifyDate(new Date());
			Map<String,String> map = projectEstimateService.updateProjectEstimateInfo(projectEstimate);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new UpdateException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	
	/**
	* @Author newway
	* @Description 删除工程概算汇总信息接口信息
	* @Date 2019年8月1日下午5:41:57
	* @Param com.tgpms.web.cost.entity.ProjectEstimate
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "删除工程概算汇总信息接口信息", notes = "删除工程概算汇总信息接口信息", httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteProjectEstimateInfo")
	public Result deleteProjectEstimateInfo(@RequestParam(value = "pcsCode", required = true) String pcsCode) {
		Result result = new Result();
		if(Common.isEmpty(pcsCode)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			Map<String,String> map = projectEstimateService.deleteProjectEstimateInfo(pcsCode);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new DeleteException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}
	
	
	/**
	* @Author newway
	* @Description 查询工程概算细项信息接口
	* @Date 2019年8月2日下午3:01:27
	* @Param pcsCode
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "查询工程概算细项信息接口", notes = "查询工程概算细项信息接口", httpMethod = "POST")
	@PostMapping(value = "/selDetailEstimateList")
	public Result selDetailEstimateList(@RequestParam(value = "selparam", required = false) String selparam,
			@RequestParam(value = "leftdition", required = false) String leftdition,
			@RequestParam(value = "condition", required = false) String condition,
			@RequestParam(value = "dataowner", required = false) String dataowner,
			@RequestParam(value = "pageNo", required = true) String pageNo,
			@RequestParam(value = "pageSize", required = true) String pageSize) {
		Result result = new Result();
		PageInfo pageInfo = new PageInfo();
		if(Common.isEmpty(pageNo) || Common.isEmpty(pageSize)) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			pageInfo.setPage(pageNo);
			pageInfo.setSize(pageSize);
			Map<String,String> map = new HashMap<String,String>();
			map.put("selparam", selparam);
			map.put("leftdition", leftdition);
			map.put("condition", condition);
			map.put("dataowner", dataowner);
			pageInfo.setQueryMap(map);
			result = detailEstimateItemsService.selDetailEstimateList(pageInfo);
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_QUERY.getExplain());
			throw new QueryException(ExceptionExplain.ERROR_BY_QUERY.getExplain());
		}
		return result;
	}
	
	
	
	
	/**
	* @Author newway
	* @Description 添加工程概算汇总信息接口
	* @Date 2019年8月1日下午4:22:08
	* @Param com.tgpms.web.cost.entity.ProjectEstimate
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "添加工程概算细项信息接口", notes = "添加工程概算细项信息接口", httpMethod = "POST")
	@PostMapping(value = "/addDetailEstimateInfo")
	public Result addDetailEstimateInfo(@RequestBody DetailEstimateItems detailEstimateItems,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(detailEstimateItems.getPcsCode())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			//获取token操作人
			detailEstimateItems.setInsertUser(tokenService.getUserName(request));
			detailEstimateItems.setInsertDate(new Date());
			Map<String,String> map = detailEstimateItemsService.addDetailEstimateInfo(detailEstimateItems);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_INSERT.getExplain());
			throw new InsertException(ExceptionExplain.ERROR_BY_INSERT.getExplain());
		}
		return result;
	}
	
	
	/**
	* @Author newway
	* @Description 更新工程概算汇总信息接口
	* @Date 2019年8月1日下午5:37:08
	* @Param com.tgpms.web.cost.entity.ProjectEstimate
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "更新工程概算细项信息接口", notes = "更新工程概算细项信息接口", httpMethod = "PUT")
	@PutMapping(value = "/updateDetailEstimateInfo")
	public Result updateDetailEstimateInfo(@RequestBody DetailEstimateItems detailEstimateItems,HttpServletRequest request) {
		Result result = new Result();
		if(Common.isEmpty(detailEstimateItems.getPcsCode()) || 
				Common.isEmpty(detailEstimateItems.getEstimateItem()) || 
				Common.isEmpty(detailEstimateItems.getSubItem())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			//获取token操作人
			detailEstimateItems.setModifyUser(tokenService.getUserName(request));
			detailEstimateItems.setModifyDate(new Date());
			Map<String,String> map = detailEstimateItemsService.updateDetailEstimateInfo(detailEstimateItems);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
			throw new UpdateException(ExceptionExplain.ERROR_BY_UPDATE.getExplain());
		}
		return result;
	}
	
	
	/**
	* @Author newway
	* @Description 删除工程概算汇总信息接口信息
	* @Date 2019年8月1日下午5:41:57
	* @Param com.tgpms.web.cost.entity.DetailEstimateItemsKey
	* @return com.tgpms.common.Result
	**/
	@ApiOperation(value = "删除工程概算细项信息接口", notes = "删除工程概算细项信息接口", httpMethod = "DELETE")
	@DeleteMapping(value = "/deleteDetailEstimateInfo")
	public Result deleteDetailEstimateInfo(@RequestBody DetailEstimateItemsKey detailEstimateItemsKey) {
		Result result = new Result();
		if(Common.isEmpty(detailEstimateItemsKey.getPcsCode()) || 
				Common.isEmpty(detailEstimateItemsKey.getEstimateItem()) || 
				Common.isEmpty(detailEstimateItemsKey.getSubItem())) {
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
			return result;
		}
		try {
			Map<String,String> map = detailEstimateItemsService.deleteDetailEstimateInfo(detailEstimateItemsKey);
			if(map.get("code").equals("1")) {
				result.setSuccess(true);
			}else {
				result.setSuccess(false);
			}
			result.setMsg(map.get("info"));
		} catch (Exception e) {
			e.printStackTrace();
			result.setSuccess(false);
			result.setMsg(ExceptionExplain.ERROR_BY_DELETE.getExplain());
			throw new DeleteException(ExceptionExplain.ERROR_BY_DELETE.getExplain());
		}
		return result;
	}

	/**
	 * @Author guan
	 * @Description 查询CWP
	 * @Date 17:39 2019-8-23
	 * @Param [pcsCode]
	 * @return com.tgpms.common.Result
	 **/
	@ApiOperation(value = "查询CWP", notes = "查询CWP", httpMethod = "POST")
	@PostMapping(value = "/querySubProject")
	public Result querySubProject(@RequestParam(value = "subProject", required = false) String subProject,
								  @RequestParam(value = "dataOwner", required = false) String dataOwner) {
		Result result = new Result();
		try {
			if(Common.isEmpty(dataOwner)) {
				result.setSuccess(false);
				result.setMsg(ExceptionExplain.EMPTY_BY_DATA.getExplain());
				return result;
			} else {
				result = subProjectsService.querySubProject(subProject, dataOwner);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	
}
